<?php

namespace App\Repository;

use App\Libs\PhpOffice\Excel;
use App\Models\Order\OrderHistoryModel;
use App\Models\Order\OrderMiddModel as model;
use App\Models\Stock\StockPhoneCheckFielModel;
use App\Repository\Commission\CommissionFileRepository;
use App\Repository\CommissionReturn\CommissionReturnLogsRepository;
use App\Repository\Order\MoreOrderRepository;
use App\Repository\Order\OrderExportRepository;
use App\Repository\Rebate\RebateLogsRepository;
use App\Repository\Stock\StockPhoneCheckRepository;
use App\Repository\UpThree\MainOrderRepostition;
use App\Repository\XhApi\PhonesRepository;
use Illuminate\Support\Facades\DB;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;

class LongFileRepository extends BaseRepository
{
    private $isType = null;
    private $log;
    private $model;

    public function __construct()
    {
        $this->log = new Logger('logFile');

        $this->log->pushHandler(
            (new RotatingFileHandler(storage_path('/logs/logFile.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
        );

        $this->model = new model();
        ini_set('max_execution_time', '1800');
        parent::__construct();
    }

    public function readDataAll($id, $type)
    {
        $res = [];
        switch ($type) {
            case 'read':
                $res = $this->readData($id);

                if ($this->isType == 'add') {

                    $this->upThreeData($id);
                }
                if ($this->isType == 'edit') {
                    $this->upEditData($id);
                }
                if ($this->isType == 'phone') {
                    $this->upEditDataPhone($id);
                }
                break;
            case 'addXhNumber':
                $model = new PhonesRepository();
                $res = $model->importPhones($id);
                break;
            case 'importRebate':
                $model = new RebateLogsRepository();
                $res = $model->importData($id);
                break;
            case 'commissionReturn':
                $model = new CommissionReturnLogsRepository();
                $res = $model->importData($id);
                break;
            case 'readPushData':
                $res = $this->readPushData($id);
                break;
            case 'readPushOrderData':
//                $this->log->info('操作readPushOrderData333333333');
                $model = new OrderExportRepository();
                $res = $model->excelPort($id);
                break;
            case 'readPushHistory':
                // 历史数据处理 导入

                break;
            case 'chartData':
                // 每天晚上 凌晨 跑一下图标数据
                break;
            case 'upThreeData':
                $res = $this->upThreeData($id);
                break;
            case 'readCommission':
                // 将佣金数据 运行到中间表中
                $model = new CommissionFileRepository();
                $res = $model->getAllCommissionData($id);
                break;
            case 'createCommission':
                // 将中间表的数据 运行到表中
                $model = new CommissionFileRepository();
                $res = $model->createCommission($id);
                break;
            case 'createCountCommission':
                // 将佣金表中的数据 运行到统计表中
                $model = new CommissionFileRepository();
                $res = $model->createCountCommission($id);
                break;
            default:
                $res = $this->upThreeData($id);
                break;
        }
        $this->log->info('参数id:' . $id . '类型:' . $type . '返回结果:' . json_encode($res));
        return $res;
    }

    // 查询数据
    public function readData($id = '')
    {
        if (empty($id)) {
            return $this->returnMsg(1, '传递参数不能为空');
        }
        $model = new MoreOrderRepository();
        $resMore = $model->getOne(['id' => $id, 'is_run' => 0]);
//        $this->log->info('文件参数:' . $id . '查询结果' . json_encode($resMore));
        if (!($resMore)) {
            return $this->returnMsg(2, '未查询到数据');
        }
        $this->isType = $resMore->type;
//        file_put_contents(storage_path('/logs/xx2.log'), json_encode($resMore));
        return $this->readLongFile($resMore->upload_rout, $resMore->type, $resMore->shop_id, $id);
    }

    // 读取文件  将文件 加入到表里面
    public function readLongFile($path, $type, $shop_id, $id = '', $fiel_name = '')
    {
        $path = public_path($path);
        // var_dump($path);
        // die();
        if (empty($path) || !file_exists($path)) {
            return $this->returnMsg(100, '文件不存在:' . $path);
        }
        if (empty($type)) {
            return $this->returnMsg(102, '类型不能为空');
        }
        if (empty($shop_id)) {
            return $this->returnMsg(103, '用户id 不存在');
        }
        $model = new Excel();
        $res = [];
        $count = 0;
        $fielCount = 0;
        if ($type == 'add') {
            $data = $model->importFile($path, ['dist_orderno', 'pay_name', 'pay_identity', 'pay_phone', 'post_sheng', 'post_shi', 'post_qu', 'post_address', 'iccid', 'card_num', 'goods_name', 'goods_code', 'author_id', 'author_name', 'sell_name'], function ($data) use ($shop_id, $id, $fiel_name) {
                $data['shop_id'] = $shop_id;
                $data['more_file_id'] = $id;
                $data['dist_name'] = $fiel_name;
                // $data['source'] = 2;
                $data['type'] = 'add';
                return $data;
            });
            // var_dump($data);
            if (!empty($data['data'])) {
                foreach (array_chunk($data['data'], 100, true) as $key => $v) {
                    $res = $this->oneInsert($v);
                }
                $count = count($data['data']);
            }
        } else if ($type == 'edit') {
//            按excel模板顺序取数据并给字段
            $data = $model->importFile($path, ['dist_orderno', 'sup_orderno', 'iccid', 'imei', 'express_company', 'express_number', 'express_desc', 'express_phone', 'status', 'act_time', 'three_msg', 'author_name', 'author_id', 'is_online', 'first_money', 'first_status_at'], function ($data) use ($shop_id, $id, $fiel_name) {
                $data['shop_id'] = $shop_id;
                $data['more_file_id'] = $id;
                $data['dist_name'] = $fiel_name;
                $data['first_money'] = empty($data['first_money']) ? 0 : floatval($data['first_money']);
                if ($data['first_money'] !== '') {
                    $data['first_status'] = 0;
                    if ($data['first_money'] > 0) {
                        $data['first_status'] = 1;
                    }
                }
                $data['type'] = 'edit';

                if (!empty($data['first_status_at'])) {
                    if (is_numeric($data['first_status_at'])) {
                        $t1 = ($data['first_status_at'] - 25569) * 3600 * 24;
                        $data['first_status_at'] = date('Y-m-d H:i:s', $t1);
                    } else if (is_string($data['first_status_at'])) {
                        $data['first_status_at'] = date('Y-m-d H:i:s', strtotime(trim($data['first_status_at'])));
                    }
                } else {
                    $data['first_status_at'] = null;
                }
                if (!empty($data['act_time'])) {
                    if (is_numeric($data['act_time'])) {
                        $t1 = ($data['act_time'] - 25569) * 3600 * 24;
                        $data['act_time'] = date('Y-m-d H:i:s', $t1);
                    } else if (is_string($data['act_time'])) {
                        $data['act_time'] = date('Y-m-d H:i:s', strtotime(trim($data['act_time'])));
                    }
                } else {
                    $data['act_time'] = '';
                }
//                充值了就是激活了
                if ($data['first_status'] == 1 && empty($data['act_time'])) {
                    $data['act_time'] = date('Y-m-d H:i:s');
                }
////                无效内容
//                if (empty($data['dist_orderno'])) {
//                    $data = null;
//                }
                return $data;
            });

            if (!empty($data['data'])) {
//                去除空值
                $data = array_filter($data['data']);
                foreach (array_chunk($data, 100, true) as $key => $v) {
                    $res = $this->oneInsert($v);
                }
                $count = count($data);
            }
        } else if ($type == 'old') {
            $data = $model->importFile($path, ['order_num', 'order', 'user_name', 'user_idcard', 'user_phone', 'province', 'city', 'area', 'address', 'iccid', 'imei', 'package_name', 'turn_package_name', 'author_id', 'author_name', 'logistics', 'logistics_num', 'channel', 'order_time', 'activat_time', 'order_status_desc', 'three_msg', 'first_money', 'first_status_at'], function ($data) use ($shop_id, $id) {
                $data['shop_id'] = $shop_id;
                $data['more_file_id'] = $id;
                $data['created_at'] = date('Y-m-d H:i:s');
                if (!empty($data['order_time']) && is_numeric($data['order_time'])) {
                    $times = intval(($data['order_time'] - 25569) * 3600 * 24);
                    $data['order_time'] = date('Y-m-d H:i:s', $times);
                }
                if (!empty($data['activat_time']) && is_numeric($data['activat_time'])) {
                    $timess = intval(($data['activat_time'] - 25569) * 3600 * 24);
                    $data['activat_time'] = date('Y-m-d H:i:s', $timess);
                }
                if (!empty($data['first_status_at'])) {
                    if (is_numeric($data['first_status_at'])) {
                        $t1 = ($data['first_status_at'] - 25569) * 3600 * 24;
                        $data['first_status_at'] = date('Y-m-d H:i:s', $t1);
                    } else if (is_string($data['first_status_at'])) {
                        $data['first_status_at'] = date('Y-m-d H:i:s', strtotime(trim($data['first_status_at'])));
                    }
                } else {
                    $data['first_status_at'] = null;
                }
                $data['first_status'] = 0;
                if ($data['first_money'] > 0) {
                    $data['first_status'] = 1;
                }
                return $data;
            });
            if (!empty($data['data'])) {
                foreach (array_chunk($data['data'], 100, true) as $key => $v) {
                    $res = $this->oneInsert($v, 'old');
                }
                $count = count($data['data']);
            }
        } else if ($type == 'phone') {
            $data = $model->importFile($path, ['check_phone', 'type_name'], function ($data) use ($shop_id, $id, $fiel_name) {
                $data['shop_id'] = $shop_id;
                $data['more_file_id'] = $id;
                // $data['dist_name'] = $fiel_name;
                if ($data['type_name'] == '移动') {
                    $data['type'] = 1;
                } else if ($data['type_name'] == '电信') {
                    $data['type'] = 2;
                } else if ($data['type_name'] == '联通') {
                    $data['type'] = 3;
                }
                // $data['source'] = 2;
                // $data['type'] = 'phone';
                return $data;
            });
            // var_dump($data);
            if (!empty($data['data'])) {
                foreach (array_chunk($data['data'], 100, true) as $key => $v) {
                    $res = $this->oneInsert($v, 'phone');
                }
                $count = count($data['data']);
            }
        }
        $update = [
            'is_run' => 1,
            'success_num' => $count,
            'fail_num' => $fielCount
        ];
        $model = new MoreOrderRepository();
        return $model->edit(['id' => $id], $update);
        // return 1;
    }

    //单个数据 插入数据表中
    public function oneInsert($data, $type = '')
    {
        // 插入老数据表中
        if ($type == 'old') {
            $model = new OrderHistoryModel();
            // $data['created_at'] = date('Y-m-d H:i:s');
            // var_dump($data);
            return $model->insert($data);
        }
        if ($type == 'phone') {
            $model = new StockPhoneCheckFielModel();
            // $data['created_at'] = date('Y-m-d H:i:s');
            // var_dump($data);
            return $model->insert($data);
        }
        // if ($type == 'edit') {
        //   $model = new OrderHistoryModel();
        //   return $model->insert($data);
        // }
        return $this->model->insert($data);
    }

    // 分批将数据提交到插入到订单表中  修改数据
    public function upEditData($id)
    {
        $where = [
            'more_file_id' => $id,
            'is_run' => 0,
            'type' => 'edit'
        ];
        $resData = $this->model->where($where)->limit(50)->get();
        // var_dump($resData, 333333);
        if (!empty($resData)) {
            $modelOrder = new MainOrderRepostition();
            $isTrue = true;
            foreach ($resData as $key => $v) {
                $isTrue = true;
                $ordersno = $distOrderno = trim($v['dist_orderno']);
                if (!empty($v['act_time']) && is_numeric($v['act_time'])) {
                    $t1 = ($v['act_time'] - 25569) * 3600 * 24;
                    $v['act_time'] = date('Y-m-d H:i:s', $t1);
                }
                if (empty($v['iccid']) && empty($v['dist_orderno'])) {
                    $isTrue = false;
                }
                // 几个导入的 字符类型判断
                if (!empty($v['status']) && !in_array($v['status'], [1, 10, 11, 20, 30, 40, 41, 50, 51, 60])) {
                    $msgs = '请传递正确的订单状态';
                    $isTrue = false;
                }
                if (!empty($v['is_online']) && !is_numeric(trim($v['is_online']))) {
                    $msgs = '是否在网,请认真填写';
                    $isTrue = false;
                }
                if ($isTrue) {
                    $threeData = [];
                    if (!empty($v['author_id'])) {
                        $threeData['author_id'] = trim($v['author_id']);
                    }
                    if (!empty($v['author_name'])) {
                        $threeData['author_name'] = trim($v['author_name']);
                    }
                    if (!empty($v['three_msg'])) {
                        $threeData['three_msg'] = trim($v['three_msg']);
                    }
                    if (!empty($v['iccid'])) {
                        $threeData['iccid'] = trim($v['iccid']);
                    }
                    if (!empty($v['imei'])) {
                        $threeData['imei'] = trim($v['imei']);
                    }
                    if (!empty($v['status'])) {
                        $threeData['status'] = intval($v['status']);
                    }
                    $where = ['ordersno' => $ordersno];
                    if (empty($v['dist_orderno'])) {
                        $where = ['iccid' => $v['iccid']];
                    }
                    if (!empty($v['iccid'])) {
                        $threeData['iccid'] = trim($v['iccid']);
                    }
                    if (!empty($v['sup_orderno'])) {
                        $threeData['sup_orderno'] = trim($v['sup_orderno']);
                    }
                    if (!empty($v['imei'])) {
                        $threeData['imei'] = trim($v['imei']);
                    }
                    if (!empty($v['act_time'])) {
                        $threeData['activat_at'] = $v['act_time'];
                        $threeData['activat_status'] = 1;
                    }
//                    只修改了订单状态为激活的情况
                    if (isset($threeData['status']) && $threeData['status'] == 40 && $threeData['activat_status'] != 1) {
                        $threeData['activat_status'] = 1;
                    }
                    if (isset($threeData['activat_status']) && $threeData['activat_status'] == 1 && empty($threeData['activat_at'])) {
                        $threeData['activat_at'] = date('Y-m-d H:i:s');
                    }
                    if (!empty($v['express_company'])) {
                        $threeData['express_company'] = trim($v['express_company']);
                    }
                    if (!empty($v['express_number'])) {
                        $threeData['express_number'] = trim($v['express_number']);
                    }
                    if (!empty($v['express_phone'])) {
                        $threeData['express_phone'] = trim($v['express_phone']);
                    }
//                    这个记录为派件员姓名
                    if (!empty($v['express_desc'])) {
                        $threeData['express_desc'] = trim($v['express_desc']);
                    }


                    if (!empty($v['is_online'])) {
                        $threeData['is_online'] = $v['is_online'];
                    }
                    if (!empty($v['first_money']) && floatval($v['first_money']) > 0) {
                        $threeData['first_money'] = $v['first_money'];
                        $threeData['first_status'] = 1;
                        $threeData['status'] = 41;
                    }
//                    只修改了订单状态为充值的情况
                    if (isset($threeData['status']) && $threeData['status'] == 41 && $threeData['first_status'] != 1) {
                        $threeData['first_status'] = 1;
                    }
                    if (!empty($v['first_status_at'])) {
                        $threeData['first_status_at'] = $v['first_status_at'];
                    }
                    if (isset($threeData['first_status']) && $threeData['first_status'] == 1 && empty($threeData['first_status_at'])) {
                        $threeData['first_status_at'] = date('Y-m-d H:i:s');
                    }
//                    print_r($threeData);
                    $resOrder = $modelOrder->followOrderStatus($where, $threeData);
                }

                // var_dump($resOrder);
                $update = [
                    'is_run' => 2,
                ];
                if (empty($resOrder['code']) || $resOrder['code'] != 200) {
                    $update = [
                        'three_msg' => !empty($resOrder['msg']) ? $resOrder['msg'] : (!empty($msgs) ? $msgs : '请传递订单号或生产号码'),
                        'is_run' => 1,
                    ];
                }
                $this->model->where(['id' => $v['id']])->update($update);
            }
        }
        if (empty($resData) || count($resData) < 50) {
            $this->createExcel($id);
            return $this->returnMsg(200, '添加完成');
        }
        // sleep(3);
        // 加入数据
        return $this->upEditData($id);
    }

    public function upThreeData($id)
    {
        $where = [
            'more_file_id' => $id,
            'status' => 0,
            'type' => 'add'
        ];
        $resData = $this->model->where($where)->limit(100)->get();
        if (!empty($resData)) {
            $modelOrder = new MainOrderRepostition();
            foreach ($resData as $key => $v) {
                $v['pary_name'] = str_replace('"', '', $v['pary_name']);
                $v['pary_name'] = str_replace('=', '', $v['pary_name']);
                $v['pary_name'] = trim($v['pary_name']);

                $v['pay_phone'] = str_replace('"', '', $v['pay_phone']);
                $v['pay_phone'] = str_replace('=', '', $v['pay_phone']);
                $v['pay_phone'] = trim($v['pay_phone']);

                $v['post_sheng'] = str_replace('"', '', $v['post_sheng']);
                $v['post_sheng'] = str_replace('=', '', $v['post_sheng']);
                $v['post_sheng'] = trim($v['post_sheng']);

                $v['post_shi'] = str_replace('"', '', $v['post_shi']);
                $v['post_shi'] = str_replace('=', '', $v['post_shi']);
                $v['post_shi'] = trim($v['post_shi']);

                $v['post_qu'] = str_replace('"', '', $v['post_qu']);
                $v['post_qu'] = str_replace('=', '', $v['post_qu']);
                $v['post_qu'] = trim($v['post_qu']);

                $v['post_address'] = str_replace('"', '', $v['post_address']);
                $v['post_address'] = str_replace('=', '', $v['post_address']);
                $v['post_address'] = trim($v['post_address']);

                $v['pay_identity'] = str_replace('"', '', $v['pay_identity']);
                $v['pay_identity'] = str_replace('=', '', $v['pay_identity']);
                $v['pay_identity'] = trim($v['pay_identity']);

                $v['goods_code'] = str_replace('"', '', $v['goods_code']);
                $v['goods_code'] = str_replace('=', '', $v['goods_code']);
                $v['goods_code'] = trim($v['goods_code']);

                $v['author_id'] = str_replace('"', '', $v['author_id']);
                $v['author_id'] = str_replace('=', '', $v['author_id']);
                $v['author_id'] = trim($v['author_id']);

                $v['author_name'] = str_replace('"', '', $v['author_name']);
                $v['author_name'] = str_replace('=', '', $v['author_name']);
                $v['author_name'] = trim($v['author_name']);
                $threeData = [
                    'name' => $v['pay_name'],
                    'contact' => $v['pay_phone'],
                    'province' => $v['post_sheng'],
                    'city' => $v['post_shi'],
                    'area' => $v['post_qu'],
                    'address' => $v['post_address'],
                    'identity' => $v['pay_identity'],
                    'shop_id' => $v['shop_id'],
                    'goods_code' => $v['goods_code'],
                    'dist_orderno' => $v['dist_orderno'],
                    'dist_id' => $id,
                    'dist_name' => $v['dist_name'],
                    'author_id' => $v['author_id'],
                    'author_name' => $v['author_name'],
                    'source' => 2
                ];
                if (!empty($v['sell_name'])) {
                    $threeData['sell_name'] = $v['sell_name'];
                }
                $resOrder = $modelOrder->orderSubmit($threeData);
                $update = [
                    'status' => 2,
                ];
                if (empty($resOrder['code']) || $resOrder['code'] != 200) {
                    $this->log->info('导入报错:' . $resOrder['msg']);
                    $update = [
                        'three_msg' => !empty($resOrder['msg']) ? $resOrder['msg'] : '',
                        'status' => 1,
                    ];
                }
                $this->model->where(['id' => $v['id']])->update($update);
            }
        }
        if (empty($resData) || count($resData) < 100) {
            $this->createExcel($id);
            return $this->returnMsg(200, '添加完成');
        }
        // sleep(3);
        // 加入数据
        return $this->upThreeData($id);
    }

    // 添加手机号 数据
    public function upEditDataPhone($id)
    {
        $where = [
            'more_file_id' => $id,
            'is_run' => 0,
        ];
        $model = new StockPhoneCheckFielModel();
        $resData = $model->where($where)->limit(50)->get();
        // var_dump($resData, 333333);
        if (!empty($resData)) {
            $modelOrder = new StockPhoneCheckRepository();
            $isTrue = true;
            foreach ($resData as $key => $v) {
                $isTrue = true;
                $insert = [
                    'check_phone' => $v->check_phone,
                    'type' => $v->type,
                    'shop_id' => $v->shop_id,
                ];
                $modelOrder->add($insert);
                $update = [
                    'is_run' => 2,
                ];
                if (empty($resOrder['code']) || $resOrder['code'] != 200) {
                    $update = [
                        'three_msg' => !empty($resOrder['msg']) ? $resOrder['msg'] : (!empty($msgs) ? $msgs : '未知错误'),
                        'is_run' => 1,
                    ];
                }
                $model->where(['id' => $v['id']])->update($update);
            }
        }
        if (empty($resData) || count($resData) < 50) {
            $this->createExcel($id);
            return $this->returnMsg(200, '添加完成');
        }
        // sleep(3);
        // 加入数据
        return $this->upEditDataPhone($id);
    }

    // 查出数据并且生成excel
    public function createExcel($id)
    {
        // $where = [
        //   'more_file_id' => $id,
        //   'is_run' => 1
        // ];
        $where = [];
        $where[] = ['more_file_id', '=', $id];
        $where[] = ['is_run', '<', 2];
        $failData = $this->model->where($where)->select(['dist_orderno', 'pay_name', 'pay_identity', 'pay_phone', 'post_sheng', 'post_shi', 'post_qu', 'post_address', 'iccid', 'card_num', 'goods_name', 'goods_code', 'author_id', 'author_name', 'express_company', 'express_number', 'three_msg'])->get()->toArray();
        $excelModel = new Excel();
        $title = [
            '订单号', '姓名', '身份证', '联系电话', '省', '市', '区', '详细地址', '入网号码', '卡号', '商品名称', '商品编号', '达人编号', '达人名称', '快递名称', '快递单号', '错误信息'
        ];
        $filename = '/attach/failFiel/' . date('Ymd') . '/' . $id . '/' . rand(100000, 999999) . '.xlsx';
        $excelModel->exportFile($failData, $title, $filename);
        $countSuccess = $this->model->where(['more_file_id' => $id, 'is_run' => 2])->count();
        $update = [
            'fail_num' => count($failData),
            'success_num' => $countSuccess,
            'fail_rout' => $filename
        ];
        $model = new MoreOrderRepository();
        return $model->edit(['id' => $id], $update);
        // $this->model->where(['id' => $id])->update();
    }
    // 推送信息处理
    // 推送过来的信息处理
    public function readPushData($id)
    {
        if (empty($id)) {
            return $this->returnMsg(1, '未查询到数据');
        }
        $res = DB::table('push_data')->where(['id' => $id])->first();
        if (empty($res)) {
            return $this->returnMsg(2, '未查询到商品');
        }
        if (!empty($res->msg)) {
            $getData = json_decode($res->msg, true);
        }
        if (empty($getData)) {
            return $this->returnMsg(3, '信息不正确');
        }
        $statusData = [
            '1' => '1', //已激活
            '4' => '20', // 已发货
            '5' => '30', //已签收
            '6' => '40', // 已激活
            '13' => '60'
        ];
        $model = new MainOrderRepostition();
        foreach ($getData['report'] as $key => $v) {
            if (!isset($statusData[$v['status']])) {
                $this->log->info('报错信息查找' . json_encode($v));
                continue;
            }
            $this->log->info('报错信息查找3333' . json_encode($v));
            // 将数据格式 修改成固定的数据格式
            $editData = [
                'status' => !empty($statusData[$v['status']]) ? $statusData[$v['status']] : '50',
                'express_company' => !empty($v['express_company']) ? $v['express_company'] : '',
                'express_number' => !empty($v['express_num']) ? $v['express_num'] : '',
                'three_msg' => !empty($v['describe']) ? $v['describe'] : '',
                'iccid' => !empty($v['iccid']) ? $v['iccid'] : '',
                'first_status' => !empty($v['balanceOne']) ? '1' : '0',
                'first_status_desc' => !empty($v['balanceOne']) ? ($v['balanceOne'] / 100) : '0',
            ];
            $where = ['orderno' => $v['order_num']];
            $this->log->info('老系统修改数据' . json_encode($where) . '参数' . json_encode($editData));
            $res = $model->followOrderStatus($where, $editData);
            $this->log->info('老系统通知参数' . json_encode($v) . '返回参数' . json_encode($res));
        }
        return $res;
    }
}
